<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Parser Grammar</title>
<link rel="stylesheet" href="../../../../../../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../../../../index.html" title="Spirit 2.5.8">
<link rel="up" href="../nonterminal.html" title="Nonterminal Parsers">
<link rel="prev" href="rule.html" title="Parser Rule">
<link rel="next" href="../numeric.html" title="Numeric Parsers">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../../../boost.png"></td>
<td align="center"><a href="../../../../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="rule.html"><img src="../../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../nonterminal.html"><img src="../../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../../index.html"><img src="../../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="../numeric.html"><img src="../../../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h5 class="title">
<a name="spirit.qi.reference.nonterminal.grammar"></a><a class="link" href="grammar.html" title="Parser Grammar">Parser Grammar</a>
</h5></div></div></div>
<h6>
<a name="spirit.qi.reference.nonterminal.grammar.h0"></a>
            <span class="phrase"><a name="spirit.qi.reference.nonterminal.grammar.description"></a></span><a class="link" href="grammar.html#spirit.qi.reference.nonterminal.grammar.description">Description</a>
          </h6>
<p>
            The grammar encapsulates a set of <a class="link" href="rule.html" title="Parser Rule">rules</a>
            (as well as primitive parsers (<a class="link" href="../parser_concepts/primitiveparser.html" title="PrimitiveParser"><code class="computeroutput"><span class="identifier">PrimitiveParser</span></code></a>) and sub-grammars).
            The grammar is the main mechanism for modularization and composition.
            Grammars can be composed to form more complex grammars.
          </p>
<h6>
<a name="spirit.qi.reference.nonterminal.grammar.h1"></a>
            <span class="phrase"><a name="spirit.qi.reference.nonterminal.grammar.header"></a></span><a class="link" href="grammar.html#spirit.qi.reference.nonterminal.grammar.header">Header</a>
          </h6>
<pre class="programlisting"><span class="comment">// forwards to &lt;boost/spirit/home/qi/nonterminal/grammar.hpp&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">spirit</span><span class="special">/</span><span class="identifier">include</span><span class="special">/</span><span class="identifier">qi_grammar</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
</pre>
<p>
            Also, see <a class="link" href="../../../structure/include.html" title="Include">Include Structure</a>.
          </p>
<h6>
<a name="spirit.qi.reference.nonterminal.grammar.h2"></a>
            <span class="phrase"><a name="spirit.qi.reference.nonterminal.grammar.namespace"></a></span><a class="link" href="grammar.html#spirit.qi.reference.nonterminal.grammar.namespace">Namespace</a>
          </h6>
<div class="informaltable"><table class="table">
<colgroup><col></colgroup>
<thead><tr><th>
                    <p>
                      Name
                    </p>
                  </th></tr></thead>
<tbody><tr><td>
                    <p>
                      <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">spirit</span><span class="special">::</span><span class="identifier">qi</span><span class="special">::</span><span class="identifier">grammar</span></code>
                    </p>
                  </td></tr></tbody>
</table></div>
<h6>
<a name="spirit.qi.reference.nonterminal.grammar.h3"></a>
            <span class="phrase"><a name="spirit.qi.reference.nonterminal.grammar.synopsis"></a></span><a class="link" href="grammar.html#spirit.qi.reference.nonterminal.grammar.synopsis">Synopsis</a>
          </h6>
<pre class="programlisting"><span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">Iterator</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">A1</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">A2</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">A3</span><span class="special">&gt;</span>
<span class="keyword">struct</span> <span class="identifier">grammar</span><span class="special">;</span>
</pre>
<h6>
<a name="spirit.qi.reference.nonterminal.grammar.h4"></a>
            <span class="phrase"><a name="spirit.qi.reference.nonterminal.grammar.template_parameters"></a></span><a class="link" href="grammar.html#spirit.qi.reference.nonterminal.grammar.template_parameters">Template
            parameters</a>
          </h6>
<div class="informaltable"><table class="table">
<colgroup>
<col>
<col>
<col>
</colgroup>
<thead><tr>
<th>
                    <p>
                      Parameter
                    </p>
                  </th>
<th>
                    <p>
                      Description
                    </p>
                  </th>
<th>
                    <p>
                      Default
                    </p>
                  </th>
</tr></thead>
<tbody>
<tr>
<td>
                    <p>
                      <code class="computeroutput"><span class="identifier">Iterator</span></code>
                    </p>
                  </td>
<td>
                    <p>
                      The underlying iterator type that the rule is expected to work
                      on.
                    </p>
                  </td>
<td>
                    <p>
                      none
                    </p>
                  </td>
</tr>
<tr>
<td>
                    <p>
                      <code class="computeroutput"><span class="identifier">A1</span></code>, <code class="computeroutput"><span class="identifier">A2</span></code>, <code class="computeroutput"><span class="identifier">A3</span></code>
                    </p>
                  </td>
<td>
                    <p>
                      Either <code class="computeroutput"><span class="identifier">Signature</span></code>,
                      <code class="computeroutput"><span class="identifier">Skipper</span></code> or
                      <code class="computeroutput"><span class="identifier">Locals</span></code> in any
                      order. See table below.
                    </p>
                  </td>
<td>
                    <p>
                      See table below.
                    </p>
                  </td>
</tr>
</tbody>
</table></div>
<p>
            Here is more information about the template parameters:
          </p>
<div class="informaltable"><table class="table">
<colgroup>
<col>
<col>
<col>
</colgroup>
<thead><tr>
<th>
                    <p>
                      Parameter
                    </p>
                  </th>
<th>
                    <p>
                      Description
                    </p>
                  </th>
<th>
                    <p>
                      Default
                    </p>
                  </th>
</tr></thead>
<tbody>
<tr>
<td>
                    <p>
                      <code class="computeroutput"><span class="identifier">Signature</span></code>
                    </p>
                  </td>
<td>
                    <p>
                      Specifies the grammar's synthesized (return value) and inherited
                      attributes (arguments). More on this here: <a class="link" href="../parser_concepts/nonterminal.html" title="Nonterminal"><code class="computeroutput"><span class="identifier">Nonterminal</span></code></a>.
                    </p>
                  </td>
<td>
                    <p>
                      <code class="computeroutput"><span class="identifier">unused_type</span></code>.
                      When <code class="computeroutput"><span class="identifier">Signature</span></code>
                      defaults to <code class="computeroutput"><span class="identifier">unused_type</span></code>,
                      the effect is the same as specifying a signature of <code class="computeroutput"><span class="keyword">void</span><span class="special">()</span></code>
                      which is also equivalent to <code class="computeroutput"><span class="identifier">unused_type</span><span class="special">()</span></code>
                    </p>
                  </td>
</tr>
<tr>
<td>
                    <p>
                      <code class="computeroutput"><span class="identifier">Skipper</span></code>
                    </p>
                  </td>
<td>
                    <p>
                      Specifies the grammar's skipper parser. Specify this if you
                      want the grammar to skip white spaces.
                    </p>
                  </td>
<td>
                    <p>
                      <code class="computeroutput"><span class="identifier">unused_type</span></code>
                    </p>
                  </td>
</tr>
<tr>
<td>
                    <p>
                      <code class="computeroutput"><span class="identifier">Locals</span></code>
                    </p>
                  </td>
<td>
                    <p>
                      Specifies the grammar's local variables. See <a class="link" href="../parser_concepts/nonterminal.html" title="Nonterminal"><code class="computeroutput"><span class="identifier">Nonterminal</span></code></a>.
                    </p>
                  </td>
<td>
                    <p>
                      <code class="computeroutput"><span class="identifier">unused_type</span></code>
                    </p>
                  </td>
</tr>
</tbody>
</table></div>
<h6>
<a name="spirit.qi.reference.nonterminal.grammar.h5"></a>
            <span class="phrase"><a name="spirit.qi.reference.nonterminal.grammar.model_of"></a></span><a class="link" href="grammar.html#spirit.qi.reference.nonterminal.grammar.model_of">Model of</a>
          </h6>
<div class="blockquote"><blockquote class="blockquote"><p>
              <a class="link" href="../parser_concepts/nonterminal.html" title="Nonterminal"><code class="computeroutput"><span class="identifier">Nonterminal</span></code></a>
            </p></blockquote></div>
<div class="variablelist">
<p class="title"><b>Notation</b></p>
<dl class="variablelist">
<dt><span class="term"><code class="computeroutput"><span class="identifier">g</span></code></span></dt>
<dd><p>
                  A grammar
                </p></dd>
</dl>
</div>
<h6>
<a name="spirit.qi.reference.nonterminal.grammar.h6"></a>
            <span class="phrase"><a name="spirit.qi.reference.nonterminal.grammar.expression_semantics"></a></span><a class="link" href="grammar.html#spirit.qi.reference.nonterminal.grammar.expression_semantics">Expression
            Semantics</a>
          </h6>
<p>
            Semantics of an expression is defined only where it differs from, or
            is not defined in <a class="link" href="../parser_concepts/nonterminal.html" title="Nonterminal"><code class="computeroutput"><span class="identifier">Nonterminal</span></code></a>.
          </p>
<div class="informaltable"><table class="table">
<colgroup>
<col>
<col>
</colgroup>
<thead><tr>
<th>
                    <p>
                      Expression
                    </p>
                  </th>
<th>
                    <p>
                      Semantics
                    </p>
                  </th>
</tr></thead>
<tbody><tr>
<td>
                    <p>
</p>
<pre xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="table-programlisting"><span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">Iterator</span><span class="special">&gt;</span>
<span class="keyword">struct</span> <span class="identifier">my_grammar</span> <span class="special">:</span> <span class="identifier">grammar</span><span class="special">&lt;</span><span class="identifier">Iterator</span><span class="special">,</span> <span class="identifier">A1</span><span class="special">,</span> <span class="identifier">A2</span><span class="special">,</span> <span class="identifier">A3</span><span class="special">&gt;</span>
<span class="special">{</span>
    <span class="identifier">my_grammar</span><span class="special">()</span> <span class="special">:</span> <span class="identifier">my_grammar</span><span class="special">::</span><span class="identifier">base_type</span><span class="special">(</span><span class="identifier">start</span><span class="special">,</span> <span class="identifier">name</span><span class="special">)</span>
    <span class="special">{</span>
        <span class="comment">// Rule definitions</span>
        <span class="identifier">start</span> <span class="special">=</span> <span class="comment">/* ... */</span><span class="special">;</span>
    <span class="special">}</span>

    <span class="identifier">rule</span><span class="special">&lt;</span><span class="identifier">Iterator</span><span class="special">,</span> <span class="identifier">A1</span><span class="special">,</span> <span class="identifier">A2</span><span class="special">,</span> <span class="identifier">A3</span><span class="special">&gt;</span> <span class="identifier">start</span><span class="special">;</span>
    <span class="comment">// more rule declarations...</span>
<span class="special">};</span>
</pre>
<p>
                    </p>
                  </td>
<td>
                    <p>
                      Grammar definition. <code class="computeroutput"><span class="identifier">name</span></code>
                      is an optional string that gives the grammar its name, useful
                      for debugging and error handling.
                    </p>
                  </td>
</tr></tbody>
</table></div>
<div class="note"><table border="0" summary="Note">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../images/note.png"></td>
<th align="left">Note</th>
</tr>
<tr><td align="left" valign="top"><p>
              The template parameters of a grammar and its start rule (the rule passed
              to the grammar's base class constructor) must match, otherwise you
              will see compilation errors.
            </p></td></tr>
</table></div>
<h6>
<a name="spirit.qi.reference.nonterminal.grammar.h7"></a>
            <span class="phrase"><a name="spirit.qi.reference.nonterminal.grammar.attributes"></a></span><a class="link" href="grammar.html#spirit.qi.reference.nonterminal.grammar.attributes">Attributes</a>
          </h6>
<div class="blockquote"><blockquote class="blockquote"><p>
              The parser attribute of the grammar is <code class="computeroutput"><span class="identifier">T</span></code>,
              its synthesized attribute. See <a class="link" href="../parser_concepts/nonterminal.html#spirit.qi.reference.parser_concepts.nonterminal.attributes"><code class="computeroutput"><span class="identifier">Attribute</span></code></a>
            </p></blockquote></div>
<h6>
<a name="spirit.qi.reference.nonterminal.grammar.h8"></a>
            <span class="phrase"><a name="spirit.qi.reference.nonterminal.grammar.complexity"></a></span><a class="link" href="grammar.html#spirit.qi.reference.nonterminal.grammar.complexity">Complexity</a>
          </h6>
<div class="blockquote"><blockquote class="blockquote"><p>
              The complexity is defined by the complexity of the its definition.
            </p></blockquote></div>
<h6>
<a name="spirit.qi.reference.nonterminal.grammar.h9"></a>
            <span class="phrase"><a name="spirit.qi.reference.nonterminal.grammar.example"></a></span><a class="link" href="grammar.html#spirit.qi.reference.nonterminal.grammar.example">Example</a>
          </h6>
<div class="note"><table border="0" summary="Note">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../images/note.png"></td>
<th align="left">Note</th>
</tr>
<tr><td align="left" valign="top"><p>
              The test harness for the example(s) below is presented in the <a class="link" href="../basics.html#spirit.qi.reference.basics.examples">Basics Examples</a>
              section.
            </p></td></tr>
</table></div>
<p>
            Some using declarations:
          </p>
<p>
</p>
<pre class="programlisting"><span class="keyword">using</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">spirit</span><span class="special">::</span><span class="identifier">ascii</span><span class="special">::</span><span class="identifier">space_type</span><span class="special">;</span>
<span class="keyword">using</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">spirit</span><span class="special">::</span><span class="identifier">int_</span><span class="special">;</span>
<span class="keyword">using</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">spirit</span><span class="special">::</span><span class="identifier">qi</span><span class="special">::</span><span class="identifier">grammar</span><span class="special">;</span>
<span class="keyword">using</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">spirit</span><span class="special">::</span><span class="identifier">qi</span><span class="special">::</span><span class="identifier">rule</span><span class="special">;</span>
</pre>
<p>
          </p>
<p>
            Basic grammar usage:
          </p>
<p>
</p>
<pre class="programlisting"><span class="keyword">struct</span> <span class="identifier">num_list</span> <span class="special">:</span> <span class="identifier">grammar</span><span class="special">&lt;</span><span class="keyword">char</span> <span class="keyword">const</span><span class="special">*,</span> <span class="identifier">space_type</span><span class="special">&gt;</span>
<span class="special">{</span>
    <span class="identifier">num_list</span><span class="special">()</span> <span class="special">:</span> <span class="identifier">base_type</span><span class="special">(</span><span class="identifier">start</span><span class="special">)</span>
    <span class="special">{</span>
        <span class="keyword">using</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">spirit</span><span class="special">::</span><span class="identifier">int_</span><span class="special">;</span>
        <span class="identifier">num</span> <span class="special">=</span> <span class="identifier">int_</span><span class="special">;</span>
        <span class="identifier">start</span> <span class="special">=</span> <span class="identifier">num</span> <span class="special">&gt;&gt;</span> <span class="special">*(</span><span class="char">','</span> <span class="special">&gt;&gt;</span> <span class="identifier">num</span><span class="special">);</span>
    <span class="special">}</span>

    <span class="identifier">rule</span><span class="special">&lt;</span><span class="keyword">char</span> <span class="keyword">const</span><span class="special">*,</span> <span class="identifier">space_type</span><span class="special">&gt;</span> <span class="identifier">start</span><span class="special">,</span> <span class="identifier">num</span><span class="special">;</span>
<span class="special">};</span>
</pre>
<p>
          </p>
<p>
            How to use the example grammar:
          </p>
<p>
</p>
<pre class="programlisting"><span class="identifier">num_list</span> <span class="identifier">nlist</span><span class="special">;</span>
<span class="identifier">test_phrase_parser</span><span class="special">(</span><span class="string">"123, 456, 789"</span><span class="special">,</span> <span class="identifier">nlist</span><span class="special">);</span>
</pre>
<p>
          </p>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright © 2001-2011 Joel de Guzman, Hartmut Kaiser<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="rule.html"><img src="../../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../nonterminal.html"><img src="../../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../../index.html"><img src="../../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="../numeric.html"><img src="../../../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
